package com.hqyj.crmdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.crmdemo.entity.DevPlan;
import com.hqyj.crmdemo.entity.RestResult;
import com.hqyj.crmdemo.entity.SaleChance;
import com.hqyj.crmdemo.service.IDevPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 何万林
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/devPlan")
@CrossOrigin
public class DevPlanController {
    @Autowired
    IDevPlanService service;

    @RequestMapping(value = "/pageName", method = RequestMethod.POST)
    public RestResult<DevPlan> queryPageName(String uName,
                                             @RequestParam(defaultValue = "1") @PathVariable("page") Integer page,
                                             @RequestParam(defaultValue = "7") @PathVariable("size") Integer size) {
        System.out.println(uName+"11***************************************************");
        QueryWrapper<DevPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_name",uName);
        Page<DevPlan> page1 = service.page(new Page<>(page, size),queryWrapper);
        System.out.println(page + "--" + size);

        Long total = page1.getTotal();
        System.out.println(total);

        return RestResult.ok(page1.getRecords(), total);
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResult<DevPlan> queryPage(@RequestParam(defaultValue = "1") @PathVariable("page") Integer page,
                                         @RequestParam(defaultValue = "7") @PathVariable("size") Integer size) {
        Page<DevPlan> page1 = service.page(new Page<>(page, size));
        System.out.println(page + "--" + size);

        Long total = page1.getTotal();
        System.out.println(total);

        return RestResult.ok(page1.getRecords(), total);
    }

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST)
    public RestResult<DevPlan> selectOne(Integer saleId) {
        System.out.println(saleId+"-------------------------------------");
        QueryWrapper<DevPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("sale_id",saleId);
        List<DevPlan> devPlans = service.queryOne(wrapper);
        return RestResult.ok(devPlans);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestResult<DevPlan> add(DevPlan devPlan) {
        boolean b = service.addDevPlan(devPlan);
        if (b) {
            return RestResult.ok("success");
        } else {
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public RestResult<DevPlan> delete(Integer saleId) {
        boolean b = service.deleteDevPlan(saleId);
        if (b) {
            return RestResult.ok("success");
        } else {
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RestResult<DevPlan> update(DevPlan devPlan) {
        boolean b = service.updateDevPlan(devPlan);
        if (b) {
            return RestResult.ok("success");
        } else {
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public RestResult<DevPlan> queryAll() {
        List<DevPlan> devPlans = service.queryAll();
        return RestResult.ok(devPlans);
    }


}
